package com.ymx.darling.datasource.defaults;

import java.util.ArrayList;
import java.util.List;

/**
 * 保存连接池状态信息
 *
 * @author 爱Java的小于
 */
public class PoolState {
    protected PooledDataSource dataSource;
    protected final List<PooledConnection> idleConnections = new ArrayList<>();
    protected final List<PooledConnection> activeConnections = new ArrayList<>();
    protected long requestCount = 0L;
    protected long accumulatedRequestTime = 0L;
    protected long accumulatedCheckoutTime = 0L;
    protected long claimedOverdueConnectionCount = 0L;
    protected long accumulatedCheckoutTimeOfOverdueConnections = 0L;
    protected long accumulatedWaitTime = 0L;
    protected long hadToWaitCount = 0L;
    protected long badConnectionCount = 0L;

    public PoolState(PooledDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public synchronized long getRequestCount() {
        return this.requestCount;
    }

    public synchronized long getAverageRequestTime() {
        return this.requestCount == 0L ? 0L : this.accumulatedRequestTime / this.requestCount;
    }

    public synchronized long getAverageWaitTime() {
        return this.hadToWaitCount == 0L ? 0L : this.accumulatedWaitTime / this.hadToWaitCount;
    }

    public synchronized long getHadToWaitCount() {
        return this.hadToWaitCount;
    }

    public synchronized long getBadConnectionCount() {
        return this.badConnectionCount;
    }

    public synchronized long getClaimedOverdueConnectionCount() {
        return this.claimedOverdueConnectionCount;
    }

    public synchronized long getAverageOverdueCheckoutTime() {
        return this.claimedOverdueConnectionCount == 0L ? 0L : this.accumulatedCheckoutTimeOfOverdueConnections / this.claimedOverdueConnectionCount;
    }

    public synchronized long getAverageCheckoutTime() {
        return this.requestCount == 0L ? 0L : this.accumulatedCheckoutTime / this.requestCount;
    }

    public synchronized int getIdleConnectionCount() {
        return this.idleConnections.size();
    }

    public synchronized int getActiveConnectionCount() {
        return this.activeConnections.size();
    }

    public synchronized String toString() {
        return "\n===CONFINGURATION==============================================" +
                "\n jdbcDriver                     " + this.dataSource.getDriver() +
                "\n jdbcUrl                        " + this.dataSource.getUrl() +
                "\n jdbcUsername                   " + this.dataSource.getUsername() +
                "\n jdbcPassword                   " + (this.dataSource.getPassword() == null ? "NULL" : "************") +
                "\n poolMaxActiveConnections       " + this.dataSource.poolMaximumActiveConnections +
                "\n poolMaxIdleConnections         " + this.dataSource.poolMaximumIdleConnections +
                "\n poolMaxCheckoutTime            " + this.dataSource.poolMaximumCheckoutTime +
                "\n poolTimeToWait                 " + this.dataSource.poolTimeToWait +
                "\n poolPingEnabled                " + this.dataSource.poolPingEnabled +
                "\n poolPingQuery                  " + this.dataSource.poolPingQuery +
                "\n poolPingConnectionsNotUsedFor  " + this.dataSource.poolPingConnectionsNotUsedFor +
                "\n ---STATUS-----------------------------------------------------" +
                "\n activeConnections              " + this.getActiveConnectionCount() +
                "\n idleConnections                " + this.getIdleConnectionCount() +
                "\n requestCount                   " + this.getRequestCount() +
                "\n averageRequestTime             " + this.getAverageRequestTime() +
                "\n averageCheckoutTime            " + this.getAverageCheckoutTime() +
                "\n claimedOverdue                 " + this.getClaimedOverdueConnectionCount() +
                "\n averageOverdueCheckoutTime     " + this.getAverageOverdueCheckoutTime() +
                "\n hadToWait                      " + this.getHadToWaitCount() +
                "\n averageWaitTime                " + this.getAverageWaitTime() +
                "\n badConnectionCount             " + this.getBadConnectionCount() +
                "\n===============================================================";
    }
}

